1 using System;
2 using
UnityEngine;
3
4 namespace
UnityStandardAssets.Characters.ThirdPerson
5 {
6     
[RequireComponent(typeof (UnityEngine.AI.NavMeshAgent))]
7     
[RequireComponent(typeof (ThirdPersonCharacter))]
8     
public class AICharacterControl : MonoBehaviour
9     {
10         
public UnityEngine.AI.NavMeshAgent agent { get; private set; } // the navmesh agent required for the path finding
11         
public ThirdPersonCharacter character { get; private set; } // the character we are controlling
12         
public Transform target; // target to aim for
13
14
15         
private void Start()
16         {
17             
// get the components on the object we need ( should not be null due to require component so no need to check )
18             agent = GetComponentInChildren<UnityEngine.AI.NavMeshAgent>();
19             character = GetComponent<ThirdPersonCharacter>();
20
21             agent.updateRotation =
false;
22             agent.updatePosition =
true;
23         }
24
25
26         
private void Update()
27         {
28             
if (target != null)
29                 agent.SetDestination(target.position);
30
31             
if (agent.remainingDistance > agent.stoppingDistance)
32                 character.Move(agent.desiredVelocity,
false, false);
33             
else
34                 character.Move(Vector3.zero,
false, false);
35         }
36
37
38         
public void SetTarget(Transform target)
39         {
40             
this.target = target;
41         }
42     }
43 }


Gõ tìm kiếm nhanh...